package com.cardgame.dao;

import com.cardgame.model.buff.Buff;
import com.cardgame.model.buff.EffectType;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Repository;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class BuffDao implements IBuffDao {
    private static final String BUFF_DATA_PATH = "data/buff.json";
    private final ObjectMapper objectMapper = new ObjectMapper();
    private final Map<Integer, Buff> buffCache = new HashMap<>();

    public BuffDao() {
        configureObjectMapper();
        loadBuffs();
    }

    private void configureObjectMapper() {
        objectMapper.registerSubtypes(Buff.class);
    }

    private void loadBuffs() {
        try {
            File file = new File(BUFF_DATA_PATH);
            if (!file.exists()) {
                List<Buff> buffs = createDefaultBuffs();
                buffCache.putAll(buffs.stream().collect(HashMap::new, (m, v) -> m.put(v.getBuffId(), v), HashMap::putAll));
            } else {
                List<Map<String, Object>> buffsList = objectMapper.readValue(file, new TypeReference<>() {
                });
                for (Map<String, Object> buffMap : buffsList) {
                    Buff buff = Buff.valueOf(buffMap);
                    buffCache.put(buff.getBuffId(), buff);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            createDefaultBuffs();
        }
    }

    private List<Buff> createDefaultBuffs() {
        List<Buff> buffs = new ArrayList<>();
        
        // 力量强化：增加攻击力
        Buff attackBoost = new Buff();
        attackBoost.setBuffId(1);
        attackBoost.setBuffDescription("力量强化：增加攻击力");
        attackBoost.setBuffEffectType(EffectType.ATTACK_BOOST);
        attackBoost.setBuffValue(1);
        attackBoost.setBuffDuration(3);
        buffs.add(attackBoost);

        // 生命强化：增加生命值
        Buff hpBoost = new Buff();
        hpBoost.setBuffId(2);
        hpBoost.setBuffDescription("生命强化：增加生命值");
        hpBoost.setBuffEffectType(EffectType.HP_BOOST);
        hpBoost.setBuffValue(1);
        hpBoost.setBuffDuration(2);
        buffs.add(hpBoost);

        // 虚弱：降低攻击力
        Buff attackReduce = new Buff();
        attackReduce.setBuffId(3);
        attackReduce.setBuffDescription("虚弱：降低攻击力");
        attackReduce.setBuffEffectType(EffectType.ATTACK_REDUCE);
        attackReduce.setBuffValue(1);
        attackReduce.setBuffDuration(2);
        buffs.add(attackReduce);

        // 生命流失：降低生命值
        Buff hpReduce = new Buff();
        hpReduce.setBuffId(4);
        hpReduce.setBuffDescription("生命流失：降低生命值");
        hpReduce.setBuffEffectType(EffectType.HP_REDUCE);
        hpReduce.setBuffValue(1);
        hpReduce.setBuffDuration(2);
        buffs.add(hpReduce);

        return buffs;
    }

    @Override
    public Buff getBuffById(Integer id) {
        if (!buffCache.containsKey(id)) {
            return null;
        }
        return buffCache.get(id);
    }

    @Override
    public List<Buff> getAllBuffs() {
        return new ArrayList<>(buffCache.values());
    }
}
